package com.anhry.app.safety.manage.zczb.service.impl;

import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserDept;
import com.anhry.app.safety.manage.zczb.bean.TbZczbBzhType;
import com.anhry.app.safety.manage.zczb.bean.TbZczbHzdCheck;
import com.anhry.app.safety.manage.zczb.bean.TbZczbXwCheck;
import com.anhry.app.safety.manage.zczb.service.ZczbService;
import com.anhry.app.safety.util.DateUtil;
import com.anhry.app.safety.util.Environment;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.UtilMethod;
import com.anhry.app.safety.util.dao.UtilDao;

/**
 * @date:2012-9-20
 * @remark:
 * @author:dongbin
 */
@Service
@SuppressWarnings("unchecked")
public class ZczbServiceImpl implements ZczbService{
	
	@Resource
	private UtilDao utilDao;

	public UtilDao getUtilDao() {
		return utilDao;
	}

	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}

	public TbZczbBzhType getBzhType(String str) throws Exception {
		List list = (List)this.utilDao.getHqlQuery("from TbZczbBzhType t where 1=1 and t.typeName='"+str+"'");
		if(null!=list && list.size()>0){
			return (TbZczbBzhType)list.get(0);
		}
		return null;
	}
	
	public String getItemXml(String hzdId,String bgdId,String yhType,String isZg) throws Exception {
		String tem = this.typeTreeInit(hzdId,bgdId,yhType,isZg).asXML();
		tem = tem.replaceAll("\\\"", "\\\\\"");
		tem = tem.replaceAll("\n", "");
		return tem;
	}
	
	public Document typeTreeInit(String hzdId,String bgdId,String yhType,String isZg){
		try {
			String t1 = "0";
			String t2 = "0";
			List tList = null;
			StringBuilder sql1 = new StringBuilder("select count(*) from tb_zczb_yh_all_view t where 1=1 and t.yh_type1=1");
			StringBuilder sql2 = new StringBuilder("select count(*) from tb_zczb_yh_all_view t where 1=1 and t.yh_type1=2");
			StringBuilder sql3 = new StringBuilder("select t.yh_type2, count(*) from tb_zczb_yh_all_view t where 1=1");
			if(null!=hzdId && hzdId.length()>0){
				sql1.append(" and t.hzd_id="+hzdId+"");
				sql2.append(" and t.hzd_id="+hzdId+"");
				sql3.append(" and t.hzd_id="+hzdId+"");
				
			}
			if(null!=bgdId && bgdId.length()>0){
				sql1.append(" and t.bgd_id="+bgdId+"");
				sql2.append(" and t.bgd_id="+bgdId+"");
				sql3.append(" and t.bgd_id="+bgdId+"");
				
			}
			if(null!=yhType && yhType.length()>0){
				sql1.append(" and t.yh_type="+yhType+"");
				sql2.append(" and t.yh_type="+yhType+"");
				sql3.append(" and t.yh_type="+yhType+"");
				
			}
			if(null!=isZg && isZg.length()>0){
				sql1.append(" and t.yh_is_zg="+isZg+"");
				sql2.append(" and t.yh_is_zg="+isZg+"");
				sql3.append(" and t.yh_is_zg="+isZg+"");
				
			}
			sql3.append(" group by t.yh_type2");
			t1 = this.utilDao.getObjectBySql(sql1.toString());
			t2 = this.utilDao.getObjectBySql(sql2.toString());
			tList = this.utilDao.getObjectBySql(sql3.toString(), 0, 0);
			Document   document = DocumentHelper.createDocument();
			Element tree = document.addElement("tree");
			tree.addAttribute("id", "0");
			Element type1= tree.addElement("item");
			if(!"0".equals(t1)){
				type1.addAttribute("text","基础管理（<font color='red'>"+t1+"</font>）");
			}else{
				type1.addAttribute("text","基础管理");
			}
			type1.addAttribute("id", "1");
			type1.addAttribute("im0", "folderClosed.gif");
			type1.addAttribute("im1", "folderOpen.gif");
			type1.addAttribute("im2", "folderClosed.gif");
			type1.addAttribute("open", "1");
			
			Element type2= tree.addElement("item");			
			if(!"0".equals(t2)){
				type2.addAttribute("text","现场管理（<font color='red'>"+t2+"</font>）");
			}else{
				type2.addAttribute("text","现场管理");
			}
			type2.addAttribute("id", "2");
			type2.addAttribute("im0", "folderClosed.gif");
			type2.addAttribute("im1", "folderOpen.gif");
			type2.addAttribute("im2", "folderClosed.gif");
			type2.addAttribute("open", "1");
			
			List allList = (List)this.utilDao.getHqlQuery("from TbZczbBzhType t where 1=1 and t.typeGrade=2 order by t.typeSort");
			if(null!=allList && allList.size()>0){
				for(int i=0;i<allList.size();i++){
					boolean b = false;
					String num = "";
					TbZczbBzhType type = (TbZczbBzhType)allList.get(i);
					if(null!=tList && tList.size()>0){
						for(int j=0;j<tList.size();j++){
							Object obj[] = (Object[])tList.get(j) ;
							if(type.getId().toString().equals(obj[0].toString())){
								b = true;
								num = obj[1].toString();
								break;
							}
						}
						if("1".equals(type.getTypeParentId().toString())){
							Element main= type1.addElement("item");
							if(b){
								main.addAttribute("text",type.getTypeName()+"（<font color='red'>"+num+"</font>）");
							}else{
								main.addAttribute("text",type.getTypeName()+"（0）");
							}
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}else{
							Element main= type2.addElement("item");			
							if(b){
								main.addAttribute("text",type.getTypeName()+"（<font color='red'>"+num+"</font>）");
							}else{
								main.addAttribute("text",type.getTypeName()+"（0）");
							}
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}
					}else{
						if("1".equals(type.getTypeParentId().toString())){
							Element main= type1.addElement("item");			
							main.addAttribute("text",type.getTypeName());
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}else{
							Element main= type2.addElement("item");			
							main.addAttribute("text",type.getTypeName());
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}
					}
				}
			}
			return document;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("ZczbServiceImpl 中 Document typeTreeInit(String hzdId,String bgdId,String yhType,String isZg)方法："+e.getMessage());
		}
		return null;
	}

	public List getYhList(String hzdId, String bgdId, String cType, String yhType, String cZg) throws Exception {
		StringBuilder hql = new StringBuilder("from TbZczbYhAllView t where 1=1 ");
		if(null!=hzdId && hzdId.length()>0){
			hql.append(" and t.hzdId="+hzdId);
		}
		if(null!=bgdId && bgdId.length()>0){
			hql.append(" and t.bgdId="+bgdId);
		}
		if(null!=cType && cType.length()>0){
			if("1".equals(cType) || "2".equals(cType)){
				hql.append(" and t.yhType1="+cType);
			}else{
				hql.append(" and t.yhType2="+cType);
			}
		}
		if(null!=yhType && yhType.length()>0){
			hql.append(" and t.yhType="+yhType);
		}
		if(null!=cZg && cZg.length()>0){
			hql.append(" and t.yhIsZg="+cZg);
		}
		hql.append(" order by t.yhType1,t.yhType2,t.yhInfoId");
		return (List)this.utilDao.getHqlQuery(hql.toString());
	}
	
	public List getYhFjList(String fjType, String yhId) throws Exception {
		StringBuilder hql = new StringBuilder("from TbZczbYhFj t where 1=1 ");
		if(null!=fjType && fjType.length()>0){
			hql.append(" and t.fjType="+fjType);
		}
		if(null!=yhId && yhId.length()>0){
			hql.append(" and t.yhId="+yhId);
		}
		hql.append(" order by t.id");
		return (List)this.utilDao.getHqlQuery(hql.toString());
	}

	public TbZczbHzdCheck getHzdCheck(TbUser user, String hzdId) throws Exception {
		try {
			String grade = user.getTbUserGrade().getId().toString();//用户级别
			TbUserDept unit = null;//用户单位
			TbUserDept dept = null;//用户部门
			if(null!=user.getTbUserUnit()){
				unit = user.getTbUserUnit();
			}
			if(null!=user.getTbUserDept()){
				dept = user.getTbUserDept();
			}
			StringBuilder hql = new StringBuilder("from TbZczbHzdCheck t where 1=1 and t.hzdId="+hzdId);
			if("5".equals(grade)){
				hql.append(" and t.unitId="+unit.getId());
			}else{
				if(null!=dept){
					hql.append(" and t.unitId="+unit.getId()+" and t.deptId="+dept.getId());
				}
			}
			List list = (List)this.utilDao.getHqlQuery(hql.toString());
			if(null!=list && list.size()>0){
				return (TbZczbHzdCheck)list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("ZczbServiceImpl: TbZczbHzdCheck getHzdCheck(TbUser user, String hzdId)异常："+e.getMessage());
		}
		return null;
	}

	public TbZczbXwCheck getXwCheck(TbUser user, String hzdId) throws Exception {
		try {
			String grade = user.getTbUserGrade().getId().toString();//用户级别
			TbUserDept unit = null;//用户单位
			TbUserDept dept = null;//用户部门
			if(null!=user.getTbUserUnit()){
				unit = user.getTbUserUnit();
			}
			if(null!=user.getTbUserDept()){
				dept = user.getTbUserDept();
			}
			StringBuilder hql = new StringBuilder("from TbZczbXwCheck t where 1=1 and t.hzdId="+hzdId);
			if("5".equals(grade)){
				hql.append(" and t.unitId="+unit.getId());
			}else{
				if(null!=dept){
					hql.append(" and t.unitId="+unit.getId()+" and t.deptId="+dept.getId());
				}
			}
			List list = (List)this.utilDao.getHqlQuery(hql.toString());
			if(null!=list && list.size()>0){
				return (TbZczbXwCheck)list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("ZczbServiceImpl: TbZczbXwCheck getXwCheck(TbUser user, String hzdId)异常："+e.getMessage());
		}
		return null;
	}

	public void zczbUp() throws Exception {
		try {
			String year = UtilMethod.getZcYear();
			String month = UtilMethod.getZcMonth();
			int half = DateUtil.currentReportPeriod();
			int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
			int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1; 
			if(currentDay == 28  && currentMonth == 2) {
				this.utilDao.deleteBySql("update tb_zczb_hzd t set t.hzd_status=1 where t.hzd_year="+year+" and t.hzd_month="+month+" and t.hzd_half=" + half) ;
				this.utilDao.deleteBySql("update tb_zczb_xw_hzd t set t.hzd_status=1 where t.hzd_year="+year+" and t.hzd_month="+month+" and t.hzd_half="+ half);
				this.utilDao.saveActionLog(null, "系统自动任务启动，将第"+year+"年 第"+month+"月的企业自查自报自动上报了>>>>" + half + "半月");
				Loggers.info("系统自动任务启动，将第"+year+"年"+month+"月的企业自查自报自动上报了>>>>" + half + "半月");
			}else if(currentDay == 15 || currentDay == 31 ) {
				this.utilDao.deleteBySql("update tb_zczb_hzd t set t.hzd_status=1 where t.hzd_year="+year+" and t.hzd_month="+month+" and t.hzd_half=" + half) ;
				this.utilDao.deleteBySql("update tb_zczb_xw_hzd t set t.hzd_status=1 where t.hzd_year="+year+" and t.hzd_month="+month+" and t.hzd_half="+ half);
				this.utilDao.saveActionLog(null, "系统自动任务启动，将第"+year+"年 第"+month+"月的企业自查自报自动上报了>>>>" + half + "半月");
				Loggers.info("系统自动任务启动，将第"+year+"年"+month+"月的企业自查自报自动上报了----31>>>>" + half + "半月" );
			}else if(currentDay == 30) {
				this.utilDao.deleteBySql("update tb_zczb_hzd t set t.hzd_status=1 where t.hzd_year="+year+" and t.hzd_month="+month+" and t.hzd_half=" + half) ;
				this.utilDao.deleteBySql("update tb_zczb_xw_hzd t set t.hzd_status=1 where t.hzd_year="+year+" and t.hzd_month="+month+" and t.hzd_half="+ half);
				this.utilDao.saveActionLog(null, "系统自动任务启动，将第"+year+"年 第"+month+"月的企业自查自报自动上报了>>>>" + half + "半月");
				Loggers.info("系统自动任务启动，将第"+year+"年"+month+"月的企业自查自报自动上报了---30 >>>>" + half + "半月" );
			}
			
			
			int endDay = Environment.getInstance().getIntProperty("corp.half.month.end");
			
			//将月报的进行上报 
				if(month.equals("1") || month.equals("3")|| month.equals("5")||
						month.equals("7")|| month.equals("8")|| month.equals("10")|| month.equals("12")) {
					if(currentDay == 31) {
						this.utilDao.deleteBySql("update tb_zczb_hzd t set t.hzd_status=1 where t.hzd_year="+year+" and t.hzd_month="+month+"and t.hzd_half is null");
						this.utilDao.deleteBySql("update tb_zczb_xw_hzd t set t.hzd_status=1 where t.hzd_year="+year+" and t.hzd_month="+month+"and t.hzd_half is null");
						this.utilDao.saveActionLog(null, "系统自动任务启动，将第"+year+"年"+month+"月的企业自查自报自动上报了！");
						Loggers.info("系统自动任务启动，将第"+year+"年"+month+"月的企业自查自报自动上报了！");
					}
				} else if(currentDay == endDay || currentMonth == 2) {
					this.utilDao.deleteBySql("update tb_zczb_hzd t set t.hzd_status=1 where t.hzd_year="+year+" and t.hzd_month="+month+"and t.hzd_half is null");
					this.utilDao.deleteBySql("update tb_zczb_xw_hzd t set t.hzd_status=1 where t.hzd_year="+year+" and t.hzd_month="+month+"and t.hzd_half is null");
					this.utilDao.saveActionLog(null, "系统自动任务启动，将第"+year+"年"+month+"月的企业自查自报自动上报了！");
					Loggers.info("系统自动任务启动，将第"+year+"年"+month+"月的企业自查自报自动上报了！");
				}
				
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("ZczbServiceImpl: zczbUp()异常："+e.getMessage());
		}
	}

}
